local skel = fk.CreateSkill {
  name = "emo__yanti",
  --tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__yanti"] = "速凝掩体",
  [":emo__yanti"] = "你造成/受到伤害时，亮出牌堆底两张牌，若均为黑色，你获得其中一张/弃置之并令此伤害-1；否则你卜算这些牌。",
  ["#emo__yanti-get"] = "速凝掩体:获得一张",

  ["$emo__yanti1"] = "速凝掩体生成",
  ["$emo__yanti2"] = "速凝掩体生成",
}

skel:addEffect(fk.DamageCaused, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(2, "bottom")
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, skel.name, nil, true, player)
    room:delay(800)
    if not player.dead then
      local remain = table.filter(cards, function (id)
        return room:getCardArea(id) == Card.Processing
      end)
      if table.every(cards, function (id)
        return Fk:getCardById(id).color == Card.Black
      end) then -- 全黑
        if #remain > 0 then
          local get = room:askToChooseCard(player, {
            target = player, skill_name = skel.name, prompt = "#emo__yanti-get",
            flag = { card_data = { { skel.name, remain } } }
          })
          room:obtainCard(player, get, true, fk.ReasonPrey, player, skel.name)
        end
        data:changeDamage(-1)
      elseif #remain > 0 then -- 卜算
        local moves = {}
        local result = room:askToGuanxing(player, { cards = remain, skill_name = skel.name, skip = true })
        if #result.top > 0 then
          table.insert(moves, {
            ids = table.reverse(result.top),
            toArea = Card.DrawPile,
            moveReason = fk.ReasonPut,
            skillName = skel.name,
            proposer = player,
            moveVisible = false,
            visiblePlayers = {player},
          })
        end
        if #result.bottom > 0 then
          table.insert(moves, {
            ids = result.bottom,
            toArea = Card.DrawPile,
            moveReason = fk.ReasonPut,
            skillName = skel.name,
            proposer = player,
            moveVisible = false,
            visiblePlayers = {player},
            drawPilePosition = -1,
          })
        end
        room:moveCards(table.unpack(moves))
      end
    end
    room:cleanProcessingArea(cards, skel.name)
  end,
})

skel:addEffect(fk.DamageInflicted, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(2, "bottom")
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, skel.name, nil, true, player)
    room:delay(800)
    if not player.dead then
      local remain = table.filter(cards, function (id)
        return room:getCardArea(id) == Card.Processing
      end)
      if table.every(cards, function (id)
        return Fk:getCardById(id).color == Card.Black
      end) then -- 全黑
        if #remain > 0 then
          room:moveCardTo(remain, Card.DiscardPile, nil, fk.ReasonDiscard, skel.name, nil, true, player)
        end
        data:changeDamage(-1)
      elseif #remain > 0 then -- 卜算
        local moves = {}
        local result = room:askToGuanxing(player, { cards = remain, skill_name = skel.name, skip = true })
        if #result.top > 0 then
          table.insert(moves, {
            ids = table.reverse(result.top),
            toArea = Card.DrawPile,
            moveReason = fk.ReasonPut,
            skillName = skel.name,
            proposer = player,
            moveVisible = false,
            visiblePlayers = {player},
          })
        end
        if #result.bottom > 0 then
          table.insert(moves, {
            ids = result.bottom,
            toArea = Card.DrawPile,
            moveReason = fk.ReasonPut,
            skillName = skel.name,
            proposer = player,
            moveVisible = false,
            visiblePlayers = {player},
            drawPilePosition = -1,
          })
        end
        room:moveCards(table.unpack(moves))
      end
    end
    room:cleanProcessingArea(cards, skel.name)
  end,
})

return skel
